package drds.common.db.xml_mapping.session;


import drds.common.db.xml_mapping.mapping.Mapper;
import drds.common.db.xml_mapping.parameter_and_result_value_type.TypeHandler;
import lombok.extern.slf4j.Slf4j;

/**
 * Specify the behavior when detects an unknown column (or unknown property parameter_and_result_value_type) of automatic mapping target.
 *
 * @author Kazuki Shimizu
 * @since 3.4.0
 */
@Slf4j
public enum AutoMappingUnknownColumnBehavior {

  /**
   * Do nothing (Default).
   */
  NONE {
    @Override
    public void doAction(Mapper MappedStatement, String columnName, String property, Class<?> propertyType) {
      // do nothing
    }
  },

  /**
   * Output warning log.
   * Note: The log level of {@code 'AutoMappingUnknownColumnBehavior'} must be set to {@code WARN}.
   */
  WARNING {
    @Override
    public void doAction(Mapper MappedStatement, String columnName, String property, Class<?> propertyType) {
      log.warn(buildMessage(MappedStatement, columnName, property, propertyType));
    }
  },

  /**
   * Fail mapping.
   * Note: throw {@link SqlSessionException}.
   */
  FAILING {
    @Override
    public void doAction(Mapper MappedStatement, String columnName, String property, Class<?> propertyType) {
      throw new SqlSessionException(buildMessage(MappedStatement, columnName, property, propertyType));
    }
  };

  /**
   * build error message.
   */
  private static String buildMessage(Mapper MappedStatement, String columnName, String property, Class<?> propertyType) {
    return new StringBuilder("Unknown column is detected on '")
      .append(MappedStatement.getId())
      .append("' auto-mapping. Mapping parameters are ")
      .append("[")
      .append("columnName=").append(columnName)
      .append(",").append("propertyName=").append(property)
      .append(",").append("propertyType=").append(propertyType != null ? propertyType.getName() : null)
      .append("]")
      .toString();
  }

  /**
   * Perform the action when detects an unknown column (or unknown property parameter_and_result_value_type) of automatic mapping target.
   *
   * @param MappedStatement current mapped statement
   * @param columnName      column name for mapping target
   * @param propertyName    property name for mapping target
   * @param propertyType    property parameter_and_result_value_type for mapping target (If this argument is not null, {@link TypeHandler} for property parameter_and_result_value_type is not registered)
   */
  public abstract void doAction(Mapper MappedStatement, String columnName, String propertyName, Class<?> propertyType);


}
